package com.estronger.shopmanage.adapter;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.estronger.shopmanage.R;
import com.estronger.shopmanage.bean.choiseDateEntity;

import java.util.List;

import cn.jesse.nativelogger.NLogger;

import static com.estronger.shopmanage.R.id.textView16;


/**
 * Created by Administrator on 2017-04-24.
 */

public class dateAdapter extends BaseAdapter {

    List<choiseDateEntity> mList;
    Context mContext;
    View.OnClickListener mListener;
    private int index = 0;

    public dateAdapter(Context context, List<choiseDateEntity> lt, View.OnClickListener listener) {
        mContext = context;
        mList = lt;
        mListener = listener;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        if (mList != null)
            return mList.size();
        else
            return 0;
    }

    public void setindex(int index){
        this.index = index;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        if (mList == null || mList.size() <= 0)
            return null;
        else
            return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        final ListItemView listItemView;

        // 初始化item view
        if (convertView == null) {
            // 通过LayoutInflater将xml中定义的视图实例化到一个View中
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.layout_adpter_choisedate, null);

            // 实例化一个封装类ListItemView，并实例化它的两个域
            listItemView = new ListItemView();
            listItemView.date = (TextView) convertView
                    .findViewById(R.id.textView);
            listItemView.img = (ImageView) convertView
                    .findViewById(R.id.imageView);
            listItemView.textView16 = (TextView) convertView.findViewById(textView16);
            listItemView.flo1 = (FrameLayout) convertView.findViewById(R.id.flo1);

            // 将ListItemView对象传递给convertView
            convertView.setTag(listItemView);
        } else {
            // 从converView中获取ListItemView对象
            listItemView = (ListItemView) convertView.getTag();
        }

        // 将资源传递给ListItemView的两个域对象
        try {
            listItemView.date.setText(Integer.parseInt(mList.get(position).getDate())+"");
            listItemView.date.setTag(mList.get(position).getTruedate());
            NLogger.i("posi" + position);
            if (position == 0) {
                listItemView.img.setImageBitmap(BitmapFactory.decodeResource(mContext.getResources(), R.drawable.u28));
                listItemView.textView16.setVisibility(View.VISIBLE);
            } else {
                listItemView.img.setImageBitmap(BitmapFactory.decodeResource(mContext.getResources(), R.drawable.u29));
                listItemView.textView16.setVisibility(View.GONE);
            }
            listItemView.flo1.setOnClickListener(mListener);
            listItemView.flo1.setTag(mList.get(position).getTruedate() + "::" + position);

            listItemView.postion = position;

        } catch (Exception e) {
            NLogger.e(""+e.getMessage());
        }

        // 返回convertView对象
        return convertView;
    }

    public void resetBg(GridView gv, int position , int preposi){
        //清楚上一个
        View v = gv.getChildAt(preposi);
        if (v != null) {
            ImageView im = (ImageView)v.findViewById(R.id.imageView);
            TextView tv = (TextView)v.findViewById(textView16);
            im.setImageBitmap(BitmapFactory.decodeResource(mContext.getResources(), R.drawable.u29));
            if(preposi == 0){
                tv.setVisibility(View.VISIBLE);
            }else{
                tv.setVisibility(View.GONE);
            }

        }

        //显示下一个
        v = gv.getChildAt(position);
        if (v != null) {
            ImageView im = (ImageView)v.findViewById(R.id.imageView);
            TextView tv = (TextView)v.findViewById(textView16);
            im.setImageBitmap(BitmapFactory.decodeResource(mContext.getResources(), R.drawable.u28));
            if(position == 0){
                tv.setVisibility(View.VISIBLE);
            }else{
                tv.setVisibility(View.GONE);
            }
        }
    }

    public static class ListItemView {
        public TextView date;
        public ImageView img;
        public TextView textView16;
        public FrameLayout flo1;
        public int postion;
    }
}
